package sun.scheduler.core.resource;

import sun.scheduler.core.resource.ResourceProfile;
import sun.scheduler.core.resource.Resource;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;

/**
 * A class that gets the {@link RessourceProfile} asssoziated with each added
 * {@link Resource}.
 *
 * @author mfj
 */
public class ResourceProfileMap implements Iterable<ResourceProfile>
{

    private HashMap<Resource, ResourceProfile> profileMap;

    /**
     * Instantiates a new {@link ResourceProfileMap} with the given capacity.
     *
     * @param capacity
     */
    public ResourceProfileMap(int capacity)
    {
        this.profileMap = new HashMap<Resource, ResourceProfile>(capacity);
    }

    /**
     * Gets the
     *
     * @param resource
     * @return
     */
    public ResourceProfile getResourceProfile(Resource resource)
    {
        return profileMap.get(resource);
    }

    /**
     * Adds a new {@link ResourceProfile} to this map.
     *
     * @param rProfile the profile to add.
     * @throws IllegalArgumentException if the {@link ResourceProfile}
     * assoziated with this {@link Resource} resource is already set.
     */
    public void add(ResourceProfile rProfile)
    {
        if (profileMap.containsKey(rProfile.getResource()))
            throw new IllegalArgumentException("This map already has a Profile, consider using set.");

        profileMap.put(rProfile.getResource(), rProfile);
    }

    /**
     * Creates and add a new {@link ResourceProfile} to this map.
     *
     * @param the resource to add.
     * @throws IllegalArgumentException if the {@link ResourceProfile}
     * assoziated with this {@link Resource} resource is already set.
     */
    public void add(Resource resource)
    {
        if (profileMap.containsKey(resource))
            throw new IllegalArgumentException("This map already has a Profile, consider using set.");

        profileMap.put(resource, new ResourceProfile(resource));
    }

    /**
     * Creates and add a new {@link ResourceProfile} to this map.
     *
     * @param the resource to add.
     * @throws IllegalArgumentException if the {@link ResourceProfile}
     * assoziated with this {@link Resource} resource is already set.
     */
    public void add(Resource resource, int maxCapacity)
    {
        if (profileMap.containsKey(resource))
            throw new IllegalArgumentException("This map already has a Profile, consider using set.");

        profileMap.put(resource, new ResourceProfile(resource, maxCapacity));
    }

    /**
     * Sets the resProfile for the given resource.
     *
     * @param resource
     * @param resProfile
     */
    public void set(Resource resource, ResourceProfile resProfile)
    {
        profileMap.put(resource, resProfile);
    }

    /**
     * Gets an iterator which considers all set and added {@link ResourceProfile}'s.
     *
     * @return
     */
    @Override
    public Iterator<ResourceProfile> iterator()
    {
        return profileMap.values().iterator();
    }
    /**
     * Gets an {@ArrayList} which contains all set and added {@link ResourceProfile}'s.
     *
     * @return
     */
    public ArrayList<ResourceProfile> asList()
    {
        return new ArrayList<ResourceProfile>(profileMap.values());
    }

    /**
     * Clears all resource profiles
     */
    public void clear()
    {
        for (ResourceProfile profile : this)
            profile.clear();
    }

}
